package com.ssm.zy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.zy.mapper.CityMapper;
import com.ssm.zy.mapper.FollowMapper;
import com.ssm.zy.mapper.RoleMapper;
import com.ssm.zy.mapper.UserMapper;
import com.ssm.zy.pojo.City;
import com.ssm.zy.pojo.Role;
import com.ssm.zy.pojo.User;
import com.ssm.zy.pojo.vo.UserVO;
import com.ssm.zy.pojo.vo.UserVos;
import com.ssm.zy.service.UserService;
import com.ssm.zy.utils.JWTUtil;
import com.ssm.zy.utils.OSSUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private FollowMapper followMapper;

    @Autowired
    private JWTUtil jwtUtil;

    @Autowired
    private CityMapper cityMapper;


    public User getcheckUser(Integer id) {
        return userMapper.getcheckUser(id);
    }

    @Override
    public String createJWTByUserInfo(User user) {
        User userForDB = userMapper.selectUserByUserName(user.getUsername());
        //判断username是否存在 && //判断password是否正确
        if (userForDB != null && userForDB.getPassword().equals(user.getPassword())) {
            //构建JWT
            Map<String,Object> claims = new HashMap<String,Object>();
            claims.put("userId",userForDB.getId());
            claims.put("userName",userForDB.getUsername());
            String loginToken = jwtUtil.createJWT(userForDB.getId() + "", "LOGIN_TOKEN", claims);
            //均没问题,就形成token,并返回
            return loginToken;
        }
        //均没问题,就形成token,并返回
        return null;
    }

    public int register(User user,Integer sheng,Integer shi,Integer qu){
        if(user.getPhoto()==null || user.getPhoto().isEmpty()){
            user.setPhoto("https://b0.bdstatic.com/0df6c8c7f109aa7b67e7cb15e6f8d025.jpg@h_1280");
        }
        user.setAddress(getAddress(sheng,shi,qu));
        return userMapper.register(user);
    }


    @Override
    public int updatePassword(Integer userId, String oldPassword, String newPassword) {
        if(oldPassword.equals(userMapper.selectUserByUserId(userId))){
            return userMapper.updatePassword(userId,newPassword);
        }else {
            return 0;
        }

    }

    @Override
    public String checkUsername(String username) {
        return userMapper.checkUsername(username);
    }

    @Override
    public String checkPhone(String phone) {
        return userMapper.checkPhone(phone);
    }


    @Override
    public UserVO getUserDetailInfo(Integer id) {
        //查询用户信息
        User user = userMapper.getcheckUser(id);
        //查询相关角色信息
        Role role = roleMapper.getRoleById(user.getRoleId());
        //创建返回值属性
        ArrayList<String> roles = new ArrayList<>();
        roles.add(role.getRoleName());

        //配置返回对象
        UserVO result = new UserVO();
        BeanUtils.copyProperties(user,result);
        result.setRoles(roles);
        return result;
    }

    //        模糊用户查询
    @Override
    public PageInfo<User> getcheckLikeUser(String likeName, Integer pageNum, Integer pageSize ) {
        PageHelper.startPage(pageNum,pageSize);
        List<User> userList=userMapper.getcheckLikeUser(likeName);
        return new PageInfo<>(userList);
    }

    @Override
    public int updateUser(User user,Integer sheng,Integer shi,Integer qu){
        user.setAddress(getAddress(sheng,shi,qu));
        return userMapper.updateUser(user);
    }

    @Override
    public int deleteUser(int userId, int myid) {
        if(myid!=userId){
            return userMapper.deleteUser(userId);
        }
        return 0;
    }

    @Override
    public int userLevelUp(Integer userId) {
        return userMapper.LevelUp(userId);
    }

    @Override
    public List<City> findCityByParentId(Integer parentId) {
        return cityMapper.findCityByParentId(parentId);
    }

    @Override
    public List<Integer> findAddress(String address) {
        String[] split = address.split("-");
        List<Integer> list=new ArrayList<Integer>();
        Integer sheng= cityMapper.findAddress(split[0]);
        if (split.length == 2) {
            list.add(sheng);
            list.add(sheng);
            list.add(cityMapper.findAddress(split[1]));
        }else{
            list.add(sheng);
            list.add(cityMapper.findAddress(split[1]));
            list.add(cityMapper.findAddress(split[2]));
        }
        return list;
    }

    @Override
    public UserVos toCheckUser(Integer userId, Integer myid) {
        UserVos userVos = userMapper.toCheckUser(userId);
        userVos.setIsFollow(followMapper.getPassiveFocusId(userVos.getId()).contains(myid) || myid == userVos.getId());
        return userVos;
    }

    private String getAddress(Integer sheng,Integer shi,Integer qu){
        String a =cityMapper.getCituNameById(sheng);
        String b =cityMapper.getCituNameById(shi);
        String c =cityMapper.getCituNameById(qu);
        if(shi.equals(sheng)){
            return a+"-"+c;
        }else{
            return a+"-"+b+"-"+c;
        }
    }
}
